
#include "CxImgProc_Imp_01.h"



#include<strstream>
using std::stringstream;

using namespace ImgProcPart;

int   CxImgProc_Imp_01::HandleImage(IplImage *pImage ,int *_incar,int *_outcar )
{
	int   Width=pImage->width;
	int   Height=pImage->height;	
	if(m_pTempImage!=NULL)
		::cvReleaseImage(&m_pTempImage);

	GrayDifference(pImage);

	Threshold((BYTE*)m_pTempImage->imageData,m_pTempImage->width,m_pTempImage->height);
	Dilation((BYTE*)m_pTempImage->imageData,m_pTempImage->width,m_pTempImage->height);
	Dilation((BYTE*)m_pTempImage->imageData,m_pTempImage->width,m_pTempImage->height);	
	Erosion((BYTE*)m_pTempImage->imageData,m_pTempImage->width,m_pTempImage->height);
	Erosion((BYTE*)m_pTempImage->imageData,m_pTempImage->width,m_pTempImage->height);
	Erosion((BYTE*)m_pTempImage->imageData,m_pTempImage->width,m_pTempImage->height);
	Erosion((BYTE*)m_pTempImage->imageData,m_pTempImage->width,m_pTempImage->height);
	Erosion((BYTE*)m_pTempImage->imageData,m_pTempImage->width,m_pTempImage->height);
	
	Judge(Width,Height);

	*_incar=this->incar;
	*_outcar=this->outcar;
	return 0;
}


int  CxImgProc_Imp_01::Judge(int  Width,int Height)
{
	
	for(int i5=50;i5<Height;i5++)	//¼ì²âÒ»Ö¡Í¼ÏñÖÐÏñËØÎª255µÄ¸öÊý
	{	
		for(int j5=0;j5<Width;j5++)
		{
			if(((BYTE*)m_pTempImage->imageData)[i5*Width+j5]==255) 
			{
				if(i5<138) in_num++;
				else out_num++; 
			}
		}
	}
		
		if(in_num>90) //½«Ç°ºóÁ½Ö¡µÄÊýÖµ·Ö±ð´æ´¢
		{
			inNum++;   //ÅÐ¶ÏÊÇ½øÀ´µÄ»¹ÊÇ³öÈ¥µÄ³µ
			a[1]=inNum;//½«µ±Ç°µÄ±ä»¯ÊýÖµ´æ´¢£¬¼´ÊÇ±ä»¯ÁË¶àÉÙ´Î
		}
		

		if(out_num>80)
		{
			outNum++;   //ÅÐ¶ÏÊÇ½øÀ´µÄ»¹ÊÇ³öÈ¥µÄ³µ
			b[1]=outNum;//½«µ±Ç°µÄ»Ò¶ÈÊýÖµ´æ´¢£¬¼´ÊÇ±ä»¯ÁË¶àÉÙ´Î
		}
		
		in_num=0;
		out_num=0;
		
		if((a[1]-a[0])==1) {incar_num++; 
		//ÓÃÀ´¼ÆÊýÇ°ºó·¢Éú±ä»¯µÄÁ½Ö¡µÄ±ä»¯ÖµÊÇ·ñÁ¬Ðø,Èç¹ûÇ°ºóÁ½Ö¡µÄÁ¬Ðø±ä»¯´ïµ½60´ÎÒÔÉÏ£¬
		//ÄÇÃ´¾ÍÈÏÎªÊÇ³µÁ¾Í¨¹ý·¢ÉúµÄ±ä»¯£¬·ñÔò¾ÍÖÃÁã
		a[0]=a[1];
		
		}
		/*		 else if (flaga<3) //Èç¹ûÃ»ÓÐÕâ¸öÑ­»·£¬ÔÚÒ»¸ö±È½Ï³¤µÄ³µÁ¾¾­¹ýÊÓÆµÖÐ£¬¿ÉÄÜÓÉÓÚÂ·ÃæºÍ¹âÏß°Ú¶¯»á³öÏÖÏñËØ±ä»¯¶ø³öÏÖÎóÅÐ¡£
		{ 
		flaga++;
		inNum++;
		
			 } */
		else if(incar_num>19)
		{
			chargeincar=incar_num;
			incar_num=0;
			flaga=0;
			inNum=0;
			a[1]=0; //Èç¹û½øÈëÁË³µÁ¾ÅÐ¶Ï²»½øÐÐ¼°Ê±ÇåÁã»áÊ¹¼ÆÊýÆ÷ÒÀÈ»¼ÆÊý£¬ÒòÎªÕâÀïÒÑ¾­½«flagÇåÁãÁË£¬
			        //ÔÚflagÑ­»·Àï»áÒ»Ö±ÔËÐÐ£¬ÒòÎª»¹ÓÐÊ®´ÎµÄÈßÓà
			a[0]=0; //µ«ÊÇÕâÑùµÄÎóÓÃ£¬È´²»»áÊ¹incarnum£¬outcarnum·¢Éú±ä»¯£¬
			        //ÒòÎªËûÊ¼ÖÕÖ»ÊÇa£¬bÊý×éÊýÖµµÄ±ä»¯Ã»ÆäËûµÄ£¬µ«ÊÇ»¹ÊÇÖÃÁã¡£
		
		}	
		else if(flaga<60)
		{
			incar_num=0;
			inNum=0;
			a[0]=0;
			a[1]=0;
			flaga++;
		}
		else
		{
			incar_num=0;
			inNum=0;
			a[0]=0;
			a[1]=0;
			flaga=0;
			chargeincar=0;
		}
	
		
		if((b[1]-b[0])==1) {outcar_num++;//ÓÃÀ´¼ÆÊýÇ°ºó·¢Éú±ä»¯µÄÁ½Ö¡µÄ±ä»¯ÖµÊÇ·ñÁ¬Ðø 
		
		b[0]=b[1];}
		/*			 else  if (flagb<3)
		{
		flagb++;
		outNum++;
			 }*/
		
		
		else if(outcar_num>19 ) 
		{
			chargeoutcar=outcar_num; //Ö»ÓÐÂú×ãÁË½ø³ö³µÌõ¼þ²Å»á¸øchargeoutcar¸³Öµ£¬±£Ö¤ÁË¾ø¶ÔÓÐ³µÁË²Å»á½øÈëÅÐ¶Ï½ø³ö³µµÄÑ­»·¡£
			outcar_num=0;
			outNum=0;
			b[1]=0;
			b[0]=0;
		}
		
		
		
		else if(flagb<60)//Èç¹ûÃ»ÓÐ³µ½øÈë¶øÇÒchargeoutcarÃ»ÓÐ¸üÐÂµÄ»°½«Êý¾Ý±£´æÁ½ÃëÖÓ
		{
			outcar_num=0;
			outNum=0;
			b[0]=0;
			b[1]=0;
			flagb++;
		}
		
		
		else
		{
			outcar_num=0;
			outNum=0;
			b[0]=0;
			b[1]=0;
			flagb=0;
			chargeoutcar=0;//·ÅÖÃÔÚ¼ì²â½áÊøºóÒÀÈ»´æÔÚ×Åchargeoutcar
		}


		//ÕâÀïÐèÒªÔÙ´ÎÅÐ¶ÏÊÇ²»ÊÇÒ»¸ö³µ»¹ÊÇÁ½¸ö³µ
		if (chargeincar>19 &&chargeoutcar>19 )//Ö»ÒªÂú×ãÁË½ø³ö³µµÄÌõ¼þ¾ÍÄÜ½øÈë½øÐÐÅÐ¶Ï
		{ 
			if (chargeoutcar>chargeincar&&chargeoutcar>35)
			{
				outcar++;
			//²»ÄÜÔÚÕâÀï·ÅÖÃ chargeoutcar=0; chargeincar=0;ÇåÁã£¬ÓÉÓÚµ±ÆäÇåÁãºóÏÂÒ»´ÎÑ­»·Ê±ÆäÖÐÒ»¸ö²»Ôö¼Ó(ÎÞÏñËØÁË)
			//ÁíÍâÒ»¸öÔö¼Ó£¨ÒÀÈ»ÓÐÏñËØ£©£¬¶øÓÖÎÞ·¨Çå³ý£¬µÚ¶þ´ÎÅÐ¶Ï»á³ö´í¡£ÐèÒª½øÐÐÇåÁã´¦Àí
					
				inNum=0;			
				outNum=0;			
				chargeoutcar=0;			
				chargeincar=0;			
				outcar_num=0;			
				incar_num=0;			
				
			}
			if(chargeoutcar<chargeincar&&chargeincar>35) //ÉèÖÃÒ»¸ö35ÊÇÎªÁË¸ü¼ÓÀ´¼õÉÙÎóÅÐ£¬ÕâÑùÔÚ½øÈëÁËÕâ¸öÑ­»·£¬ÒÀÈ»¿ÉÄÜÊÇ·´¹âµÄ
			//µÄÓ°Ïì£¬µ«ÊÇËûµÄÖµÒ»°ã²»»á´óÓÚ35£¬ËùÒªÒª´óÓÚÁË35²ÅÄÜ½øÐÐ¼ÆÊý£¬Õâ¸ö35ÒÀÈ»ÊÇ°´ÕÕÎÒÃÇµÚÈýÁ¾½øÈë³µÉè¶¨¡£
		
			{
			
				incar++;			
				inNum=0;			
				outNum=0;			
				outcar_num=0;			
				incar_num=0;			
				chargeoutcar=0;			
				chargeincar=0;			
				
			}
			else
			{
				inNum=0;			
				outNum=0;			
				outcar_num=0;		
				incar_num=0;			
				chargeoutcar=0;			
				chargeincar=0;
			
			} //µ«ÊÇÓÉÓÚÎó½øÑ­»·ºóµÄÖµ»¹ÔÚ£¬Ã»ÓÐ³µÁ¾¼ÆÊý¾ÍÃ»ÓÐÇåÁã£¬ÐèÒªÈËÎªµÄÇåÁã		
		}

		else if (chargeincar>65 ) //»òÕßÓÉÓÚÆäËûÎóÅÐÊ¹µÃ²»ÄÜ½øÈëÉÏÃæÅÐ¶ÏµÄ»°£¬ÔÚ´óÓÚÁ¬Ðø80Ö¡Ò²¿ÉÈÏÎªÊÇ³µÁ¾
		{
			incar++;			
			inNum=0;
			outNum=0;
			outcar_num=0;
			incar_num=0;
			chargeoutcar=0;
			chargeincar=0;			
		
		}


		else if(chargeoutcar>65 )
		{
			outcar++;
			outcar_num=0; //ÔÚÒÑ¾­¼ÆÊýÁËµÄÇé¿öÏÂ Ò»¶¨Òª½«Ö®Ç°Êý¾ÝÈ«²¿ÇåÁã  ²»È»¾ÍÈÝÒ×³öÏÖÇ°ÃæÊý¾ÝÃ»Çå³ýµÄÖØ¸´¼ÆÊý
			incar_num=0;
			inNum=0;
			outNum=0;
			chargeoutcar=0;
			chargeincar=0;			
		}
		return 0;
}


void   CxImgProc_Imp_01::GrayDifference(IplImage *pImage)
{
	IplImage *pSource=pImage;
	IplImage *pBackground=m_Background;
	int	Width=pSource->width;
	int	Height=pSource->height;
	CvSize size;
	size.height=Height;
	size.width=Width;
	m_pTempImage=::cvCreateImage(size,8,1);
	BYTE* imageData;
	BYTE* tempData;
	BYTE* backData;

	for(int i=0;i<Height;i++)
	{
		imageData=(unsigned char*)(pSource->imageData+i*pSource->widthStep);
		tempData =(unsigned char*)(m_pTempImage->imageData+i*m_pTempImage->widthStep);
		backData =(unsigned char*)(pBackground->imageData+i*pBackground->widthStep);
		for(int j=0;j<Width;j++)
		{	
			tempData[j]=abs(imageData[3*j+2]-backData[3*j+2])*0.299+
				abs(imageData[3*j+1]-backData[3*j+1])*0.587+ 
				abs(imageData[3*j]-backData[3*j])*0.114;	
		}

	}
}




	
void CxImgProc_Imp_01::Threshold(BYTE * pColorData,int Width,int Height)
{
	int i,j,t,k,t1,maxb;
	float p[256],u=0.0,uu[256],w[256],b[256],max;
	//long pp[256];
	

	for(i=0;i<256;i++)
		p[i]=0;

	for(i=0;i<Height;i++){	
		for(j=0;j<Width;j++){
			p[pColorData[Width*i+j]]+=1;

		}     
	}

	for(i=0;i<256;i++)
	{	uu[i]=0;
	w[i]=0;
	b[i]=0;

	}
	for (t=0;t<256;t++)//¼ÆËã»Ò¶ÈÖ±·½Í¼¼´¸÷»Ò¶ÈÖµËùÕ¼±ÈÀý
	{   //printf("%f     ",p[t]);
		p[t]=p[t]/(Width*Height);
		//printf("%f     ",p[t]);
	}
	for (t=1;t<256;t++)//Çó»Ò¶ÈÆ½¾ùÖµ
	{
		u=u+(t-1)*p[t-1];
		//	printf("%f     ",u);
	}
	for (k = 1; k < 256; k++)
	{
		uu[k] = uu[k-1] + (k-1) * p[k-1];//»Ò¶ÈÀà¾ùÖµ
		w[k] = w[k-1] + p[k-1];
		if ((w[k] * (1-w[k])) != 0)
			b[k]=((u*w[k]-uu[k])*(u*w[k]-uu[k]))/(w[k]*(1-w[k]));//¼ÆËãÀà·ÖÀëÖ¸±ê
		//	printf("%f     ",b[k]);
	}
	max=0;
	for (k=0;k<256; k++)//Çó·ÖÀëÖ¸±êµÄ×î´óÖµ
	{
		if (b[k] >= max)
		{max = b[k];
		maxb = k;}
		//		 printf("%f  %f ",max,b[k]);
	}
	t1 =maxb-1;//×î¼ÑãÐÖµ
int	bir=t1;
	// printf("%d ",t1);
	//t1=20;
	for(i=0; i<Width*Height; i++)
	{
		pColorData[i]=pColorData[i]>t1?255:0;
	}	
}




void CxImgProc_Imp_01::Erosion(BYTE* pColorData,int width,int height){
	int i,j;
	BYTE    *pColorData2=(BYTE *)malloc(height*width); //ad  
	memset(pColorData2,0,height*width);//ad  
	for(i=0;i<height-1;i++)
		for(j=0;j<width-1;j++) pColorData2[i*width+j]=pColorData[i*width+j];
	for(i=1;i<height-1;i++)
		for(j=1;j<width-1;j++){

			if(pColorData2[(i-1) *width+j-1]==0)
				pColorData[i*width+j]=0;
			if(pColorData2[(i-1)*width+j]==0) pColorData[i*width+j]=0;
			if(pColorData2[(i-1)*width+j+1]==0) pColorData[i*width+j]=0;
			if(pColorData2[(i)*width+j-1]==0) pColorData[i*width+j]=0;
			if(pColorData2[(i)*width+j]==0) pColorData[i*width+j]=0;
			if(pColorData2[(i)*width+j+1]==0) pColorData[i*width+j]=0;
			if(pColorData2[(i+1)*width+j-1]==0) pColorData[i*width+j]=0;
			if(pColorData2[(i+1)*width+j]==0) pColorData[i*width+j]=0;
			if(pColorData2[(i+1)*width+j+1]==0) pColorData[i*width+j]=0;
		}
     free(pColorData2); //Èç¹û²»ÊÍ·ÅÄÚ´æ»á³öÏÖÄÚ´æµÄÒç³ö£¬ÔÚÕâÀïÏµÍ³»áÒ»Ö±ÉêÇëÄÚ´æ
     pColorData2=NULL;  //½«ÊÍ·ÅµÄÄÚ´æÖ¸Áã£¬·ÀÖ¹ÂÒÓÃ
}






void CxImgProc_Imp_01::Dilation(BYTE* pColorData,int width,int height){
	int i,j;
	BYTE    *pColorData2=(BYTE *)malloc(height*width); //ad  
	memset(pColorData2,0,height*width);//ad  
	for(i=0;i<height-1;i++)
		for(j=0;j<width-1;j++) pColorData2[i*width+j]=pColorData[i*width+j];

	for(i=1;i<height-1;i++)
		for(j=1;j<width-1;j++){
			if(pColorData2[(i-1)*width+(j-1)]==255)pColorData[(i)*width+(j)]=255;
			if(pColorData2[(i-1)*width+(j)]==255)pColorData[(i)*width+(j)]=255;
			if(pColorData2[(i-1)*width+(j+1)]==255)pColorData[(i)*width+(j)]=255;
			if(pColorData2[(i)*width+(j-1)]==255)pColorData[(i)*width+(j)]=255;
			if(pColorData2[(i)*width+(j)]==255)pColorData[(i)*width+(j)]=255;
			if(pColorData2[(i)*width+(j+1)]==255)pColorData[(i)*width+(j)]=255;
			if(pColorData2[(i+1)*width+(j-1)]==255)pColorData[(i)*width+(j)]=255;
			if(pColorData2[(i+1)*width+(j)]==255)pColorData[(i)*width+(j)]=255;
			if(pColorData2[(i+1)*width+(j+1)]==255)pColorData[(i)*width+(j)]=255;
		}
    free(pColorData2);   //Èç¹û²»ÊÍ·ÅÄÚ´æ»á³öÏÖÄÚ´æµÄÒç³ö£¬ÔÚÕâÀïÏµÍ³»áÒ»Ö±ÉêÇëÄÚ´æ
     pColorData2=NULL;
}




